package com.icxcc.common.framework.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.icxcc.common.framework.mapper.SuperMapper;

import java.util.Map;

public class SuperServiceImpl<T> implements SuperService<T> {
    private SuperMapper<T> mapper;

    public SuperMapper<T> getMapper() {
        return mapper;
    }

    public SuperServiceImpl(SuperMapper<T> mapper) {
        this.mapper = mapper;
    }

    @Override
    public Page<T> page(Map<String,Object> param) {
        Page<T> page;
        if (param.containsKey("page")&&param.containsKey("limit")){
            page = new Page<>(Long.valueOf((String) param.get("page")),Long.valueOf((String)param.get("limit")));
        }else{
            page = new Page<>(1,Integer.MAX_VALUE);
        }
        if (param.containsKey("sort")){
            if (param.containsKey("order")&& "desc".equalsIgnoreCase((String)param.get("order"))){
                page.setDescs((String)param.get("sort"));
            }else{
                page.setAscs((String)param.get("sort"));
            }
        }
        return (Page<T>)page.setRecords(mapper.list(param, page));
    }

    @Override
    public int save(T t) {
        return mapper.save(t);
    }

    @Override
    public int update(T t) {
        return mapper.update(t);
    }

    @Override
    public int delete(T t) {
        return mapper.delete(t);
    }

    @Override
    public int deleteByPrimarykey(long id) {
        return mapper.deleteByPrimarykey(id);
    }

    @Override
    public T getByPrimarykey(long id) {
        return mapper.getByPrimarykey(id);
    }
}
